package 程序员面试金典;

import org.junit.Test;

import java.util.LinkedList;

public class 二进制数转字符串 {

    public String printBin(double num) {
        StringBuilder builder = new StringBuilder();
        while (num%1!=0){
            double m = num * 2;
            int i = m >= 1 ? 1 : 0;
            builder.append(i);
            num=m-i;
        }
        if (builder.length()>30)
            return "ERROR";
       return "0."+builder.toString();
    }
    @Test
    public void test(){
        int num=4;
        //从最低位开始进行与运算
        System.out.println((num & 1) == 1);
        System.out.println(Integer.toBinaryString(num));
        num>>>=1;
        System.out.println(Integer.toBinaryString(num));
    }
}
